package chatRoom2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receiver implements Runnable{

    private DatagramSocket socket;

    public Receiver(DatagramSocket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        while(true){
            try {
                byte[] buf=new byte[1024];
                DatagramPacket packet=new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                String msg=new String(packet.getData(),0,packet.getLength());
                System.out.println("来自发送端："+packet.getAddress()+"  信息内容为："+msg);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }



    }
}
